package com.example.chatweb.controller;

import com.example.chatweb.entity.EntityRelation;
import com.example.chatweb.entity.Tag;
import com.example.chatweb.mapper.EntityRelationMapper;
import com.example.chatweb.request.InsertEntityRequest;
import com.example.chatweb.service.EntityRelationService;
import com.example.chatweb.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


//import static jdk.nashorn.internal.objects.Global.print;

@RestController
public class EntityRelationController {
    private final EntityRelationService entityRelationService;
    private final EntityRelationMapper entityRelationMapper;

    @Autowired()
    public EntityRelationController(EntityRelationService entityRelationService, EntityRelationMapper entityRelationMapperMapper) {
        this.entityRelationService = entityRelationService;
        this.entityRelationMapper = entityRelationMapperMapper;
    }
    public boolean saveUniqueContent(List<EntityRelation> entityRelations) {
        // 获取数据库中已存在的content
        List<EntityRelation> existingEntities = entityRelationMapper.selectList(null);
        List<String> existingContents = existingEntities.stream()
                .map(EntityRelation::getContent)
                .collect(Collectors.toList());

        // 过滤掉已存在的content
        List<EntityRelation> newEntities = entityRelations.stream()
                .filter(entity -> !existingContents.contains(entity.getContent()))
                .collect(Collectors.toList());

        // 批量插入新的EntityRelation
        if (!newEntities.isEmpty()) {
            System.out.println("输出实体");
            System.out.println(newEntities);
            return entityRelationService.saveBatch(newEntities);
        }
        return  true;
    }

    @PostMapping("/insertEntity")
    public R insertEntity(@RequestBody InsertEntityRequest insertEntityRequest){
        boolean i = saveUniqueContent(insertEntityRequest.getEntityRelation());
        System.out.println(insertEntityRequest.getEntityRelation());
        if(i){
            return R.ok("添加实体成功");
        }else{
            return R.failure("添加实体失败");
        }
    }
    public Map<String, List<Map<String, String>>> getDistinctEntitiesByTags() {
        List<EntityRelation> entities = entityRelationMapper.getDistinctEntitiesByTags();
        return entities.stream()
                .collect(Collectors.groupingBy(EntityRelation::getLabel,
                        Collectors.mapping(entity -> {
                            Map<String, String> map = new HashMap<>();
                            map.put("id", entity.getId().toString());
                            map.put("label", entity.getLabel());
                            map.put("content", entity.getContent());
                            return map;
                        }, Collectors.toList())));
    }

    @GetMapping("/conceptLibrary")
    public Map<String, List<Map<String, String>>> getContentsByTags() {
        System.out.println("概念库");
        System.out.println(getDistinctEntitiesByTags());
        return getDistinctEntitiesByTags();
    }

    @DeleteMapping("/delete_entity_relation/{id}")
    public R deleteEntityRelation(@PathVariable String id) {
        System.out.println("删除");
        System.out.println(id);
        boolean result = entityRelationService.deleteEntityRelationById(id);
        if(result){
            return R.ok("删除标签成功");
        }else{
            return R.failure("删除标签失败");
        }
    }

    @PostMapping("/add_entity_relation")
    public R addEntityRelation(@RequestBody EntityRelation entityRelation) {
        boolean result = entityRelationService.saveEntityRelation(entityRelation);
        System.out.println("新增");
        System.out.println(entityRelation);
        if(result){
            return R.ok("新增标签成功");
        }else{
            return R.failure("新增标签失败");
        }
    }
}
